package com.huacai.restbooking.service.impl;

import java.util.List;
import com.huacai.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huacai.restbooking.mapper.RestaurantBookingMapper;
import com.huacai.restbooking.domain.RestaurantBooking;
import com.huacai.restbooking.service.IRestaurantBookingService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 餐厅预订Service业务层处理（更新）
 *
 * @author huacai
 * @date 2025-06-16
 */
@Service
public class RestaurantBookingServiceImpl implements IRestaurantBookingService
{
    @Autowired
    private RestaurantBookingMapper restaurantBookingMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询餐厅预订
     *
     * @param id 餐厅预订主键
     * @return 餐厅预订
     */
    @Override
    public RestaurantBooking selectRestaurantBookingById(Long id)
    {
        return restaurantBookingMapper.selectRestaurantBookingById(id);
    }

    /**
     * 查询餐厅预订列表
     *
     * @param restaurantBooking 餐厅预订
     * @return 餐厅预订
     */
    @Override
    public List<RestaurantBooking> selectRestaurantBookingList(RestaurantBooking restaurantBooking)
    {
        return restaurantBookingMapper.selectRestaurantBookingList(restaurantBooking);
    }

    /**
     * 新增餐厅预订
     *
     * @param restaurantBooking 餐厅预订
     * @return 结果
     */
    @Override
    public int insertRestaurantBooking(RestaurantBooking restaurantBooking)
    {
        restaurantBooking.setCreateTime(DateUtils.getNowDate());
        return restaurantBookingMapper.insertRestaurantBooking(restaurantBooking);
    }

    /**
     * 批量新增餐厅预订
     *
     * @param restaurantBookings 餐厅预订List
     * @return 结果
     */
    @Override
    public int batchInsertRestaurantBooking(List<RestaurantBooking> restaurantBookings)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(restaurantBookings)) {
            try {
                for (int i = 0; i < restaurantBookings.size(); i++) {
                    int row = restaurantBookingMapper.insertRestaurantBooking(restaurantBookings.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == restaurantBookings.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改餐厅预订
     *
     * @param restaurantBooking 餐厅预订
     * @return 结果
     */
    @Override
    public int updateRestaurantBooking(RestaurantBooking restaurantBooking)
    {
        restaurantBooking.setUpdateTime(DateUtils.getNowDate());
        return restaurantBookingMapper.updateRestaurantBooking(restaurantBooking);
    }

    /**
     * 批量删除餐厅预订
     *
     * @param ids 需要删除的餐厅预订主键
     * @return 结果
     */
    @Override
    public int deleteRestaurantBookingByIds(Long[] ids)
    {
        return restaurantBookingMapper.deleteRestaurantBookingByIds(ids);
    }

    /**
     * 删除餐厅预订信息
     *
     * @param id 餐厅预订主键
     * @return 结果
     */
    @Override
    public int deleteRestaurantBookingById(Long id)
    {
        return restaurantBookingMapper.deleteRestaurantBookingById(id);
    }
}
